package com.sgcc.statistics.service.impl;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import com.sgcc.common.core.domain.R;
import com.sgcc.common.core.utils.DateUtils;
import com.sgcc.common.security.service.TokenService;
import com.sgcc.statistics.domain.dto.IndexMapBindDto;
import com.sgcc.statistics.domain.dto.IndexMapBindExDto;
import com.sgcc.statistics.domain.dto.OrgAndAreaInfoDto;
import com.sgcc.statistics.utils.PercentUtils;
import com.sgcc.system.api.RemoteAreaService;
import com.sgcc.system.api.RemoteDeptService;
import com.sgcc.system.api.RemoteDictDataService;
import com.sgcc.system.api.domain.SysArea;
import com.sgcc.system.api.domain.SysDept;
import com.sgcc.system.api.domain.SysUser;
import com.sgcc.system.api.model.LoginUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sgcc.statistics.mapper.OOperationIndexMapper;
import com.sgcc.statistics.domain.OOperationIndex;
import com.sgcc.statistics.service.IOOperationIndexService;

/**
 * OOperationIndexService业务层处理
 *
 * @author sgcc
 * @date 2021-04-13
 */
@Service
public class OOperationIndexServiceImpl implements IOOperationIndexService {
    @Autowired
    private OOperationIndexMapper oOperationIndexMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RemoteDictDataService remoteDictDataService;
    @Autowired
    private RemoteAreaService remoteAreaService;

    /**
     * 查询OOperationIndex
     *
     * @param id OOperationIndexID
     * @return OOperationIndex
     */
    @Override
    public OOperationIndex selectOOperationIndexById(String id) {
        return oOperationIndexMapper.selectOOperationIndexById(id);
    }

    /**
     * 查询OOperationIndex列表
     *
     * @param oOperationIndex OOperationIndex
     * @return OOperationIndex
     */
    @Override
    public List<OOperationIndex> selectOOperationIndexList(OOperationIndex oOperationIndex) {
        return oOperationIndexMapper.selectOOperationIndexList(oOperationIndex);
    }

    /**
     * 新增OOperationIndex
     *
     * @param oOperationIndex OOperationIndex
     * @return 结果
     */
    @Override
    public int insertOOperationIndex(OOperationIndex oOperationIndex) {
        LoginUser loginUser = tokenService.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        oOperationIndex.setId(id);
        R idstr = remoteDictDataService.getIdStr("same_day_id_create", "o_operation_index");
        if (idstr.getCode() == 200) {
            oOperationIndex.setIndexNumber(idstr.getData().toString());
        }
        oOperationIndex.setMaintainUnit(sysUser.getDeptId());
        oOperationIndex.setCreateBy(sysUser.getUserName());
        oOperationIndex.setUpdateBy(sysUser.getUserName());
        oOperationIndex.setCreateTime(DateUtils.getNowDate());
        oOperationIndex.setUpdateTime(DateUtils.getNowDate());
        return oOperationIndexMapper.insertOOperationIndex(oOperationIndex);
    }

    /**
     * 修改OOperationIndex
     *
     * @param oOperationIndex OOperationIndex
     * @return 结果
     */
    @Override
    public int updateOOperationIndex(OOperationIndex oOperationIndex) {
        LoginUser loginUser = tokenService.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        oOperationIndex.setMaintainUnit(sysUser.getDeptId());
        oOperationIndex.setUpdateBy(sysUser.getUserName());
        oOperationIndex.setUpdateTime(DateUtils.getNowDate());
        return oOperationIndexMapper.updateOOperationIndex(oOperationIndex);
    }

    /**
     * 批量删除OOperationIndex
     *
     * @param ids 需要删除的OOperationIndexID
     * @return 结果
     */
    @Override
    public int deleteOOperationIndexByIds(String[] ids) {
        return oOperationIndexMapper.deleteOOperationIndexByIds(ids);
    }

    /**
     * 删除OOperationIndex信息
     *
     * @param id OOperationIndexID
     * @return 结果
     */
    @Override
    public int deleteOOperationIndexById(String id) {
        return oOperationIndexMapper.deleteOOperationIndexById(id);
    }

//    @Override
//    public List<IndexMapBindDto> indexMapBind(Map<String, Object> condition) {
//        String statisticalTime = (String) condition.get("statisticalTime");
//        if (StringUtils.isEmpty(statisticalTime)) {
//            //当天
////            statisticalTime = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
//            //取前一天的数据
//            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//            Calendar calendar = Calendar.getInstance();
//            calendar.set(Calendar.HOUR_OF_DAY, -24);
//            statisticalTime = dateFormat.format(calendar.getTime());
//        }
//        statisticalTime = statisticalTime.substring(0, 10);
//        condition.put("statisticalTime", statisticalTime);
//        List<IndexMapBindDto> result = new ArrayList<>();
//        String orgNo = (String) condition.get("orgNo");
//        if (org.apache.commons.lang3.StringUtils.isEmpty(orgNo)) {
//            SysUser user = tokenService.getLoginUser().getSysUser();
//            orgNo = user.getDeptId();
//            condition.put("orgNo", orgNo);
//        }
//        R<SysArea> resutl = remoteAreaService.getAreaByDeptId(orgNo);
//        SysArea area = resutl.getData();
//        List<String> orgNos = new ArrayList<>();
//        // type:4为县公司人员
//        String type = area.getType();
////        if ("4".equals(type)) {
////            orgNos.add(orgNo);
////            //县公司无下一级
////            for (IndexMapBindDto item : result) {
////                item.setLevelFlag(0);
////            }
////        }else {
////            List<Office> orgChildren = officeMapper.getChildrenByIdAndType(company.getId(), "1");
////            if (ListUtil.isNotEmpty(orgChildren) && null != orgChildren.get(0)) {
////                for (Office orgChild : orgChildren) {
////                    orgNos.add(orgChild.getCode());
////                }
////            }else {
////                return result;
////            }
////        }
//        if ("1".equals(type) || "2".equals(type) || "3".equals(type)) {
//            List<SysDept> orgChildren = oOperationIndexMapper.getChildrenByIdAndType(orgNo, "01");
//            if (orgChildren != null && orgChildren.size()>0) {
//                for (SysDept orgChild : orgChildren) {
//                    orgNos.add(orgChild.getDeptId());
//                }
//            } else {
//                return result;
//            }
//        } else {
//            orgNos.add(orgNo);
//        }
//        List<OrgAndAreaInfoDto> children = oOperationIndexMapper.getAreaByIds(orgNos);
//        if (children != null && children.size()>0) {
//            for (OrgAndAreaInfoDto child : children) {
//                IndexMapBindDto item = new IndexMapBindDto();
//                item.setAreaId(child.getAreaId());
//                item.setAreaName(child.getAreaName());
//                item.setOrgNo(child.getOrgNo());
//                item.setOrgName(child.getOrgName());
//                condition.put("subOrgNo", child.getOrgNo());
//                List<IndexMapBindDto> datas = oOperationIndexMapper.indexMapBind(condition);
//                if (datas != null && datas.size()>0) {
//                    IndexMapBindDto data = datas.get(0);
//                    BigDecimal rate = data.getRate();
//                    Long nCons = data.getnCons();
//                    Long bindAllNum = data.getBindAllNum();
//                    item.setRate(rate);
//                    item.setRateStr(PercentUtils.getPercentNum(rate));
//                    item.setnCons(nCons);
//                    item.setBindAllNum(bindAllNum);
//                }
//                result.add(item);
//            }
//        }
//        if (!"1".equals(type) && !"2".equals(type)) {
//            //县公司无下一级
//            for (IndexMapBindDto item : result) {
//                item.setLevelFlag(0);
//            }
//        }
//        return result;
//    }


    @Override
    public List<IndexMapBindExDto> indexMapBind(Map<String, Object> condition) {
        String statisticalTime = (String) condition.get("statisticalTime");
        if (StringUtils.isEmpty(statisticalTime)) {
            //当天
//            statisticalTime = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
            //取前一天的数据
            DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar=Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY,-24);
            statisticalTime = dateFormat.format(calendar.getTime());
        }
        statisticalTime = statisticalTime.substring(0, 10);
        condition.put("statisticalTime", statisticalTime);
        List<IndexMapBindExDto> result = new ArrayList<>();
        String orgNo = (String) condition.get("orgNo");
        if (org.apache.commons.lang3.StringUtils.isEmpty(orgNo)) {
            SysUser user = tokenService.getLoginUser().getSysUser();
            orgNo = user.getDeptId();
            condition.put("orgNo", orgNo);
        }
        SysDept company = oOperationIndexMapper.getDeptByCode(orgNo);
        String nowOrgCode = company.getDeptId();
        String nowOrgName = company.getDeptName();
        R<SysArea> resutl = remoteAreaService.getAreaByDeptId(orgNo);
        SysArea area = resutl.getData();
        List<String> orgNos = new ArrayList<>();
        // type:4为县公司人员
        Integer orgtype = company.getGrade();
        String type = area.getType();
        String areaId = area.getId();
        if (orgtype<4) {
            List<SysDept> orgChildren = oOperationIndexMapper.getChildrenByIdAndType(orgNo, "01");
            if (orgChildren!=null && orgChildren.size()>0) {
                for (SysDept orgChild : orgChildren) {
                    orgNos.add(orgChild.getDeptId());
                }
            }else {
                return result;
            }
        }else {
//            orgNos.add(orgNo);
//            company = company.getParent();
            company = oOperationIndexMapper.getDeptByCode(company.getParentId());
            nowOrgCode = company.getDeptId();
            nowOrgName = company.getDeptName();
            resutl = remoteAreaService.getAreaByDeptId(company.getDeptId());
            areaId = resutl.getData().getId();
            List<SysDept> orgChildren = oOperationIndexMapper.getChildrenByIdAndType(company.getDeptId(), "01");
            if (orgChildren!=null && orgChildren.size()>0) {
                for (SysDept orgChild : orgChildren) {
                    orgNos.add(orgChild.getDeptId());
                }
            }else {
                return result;
            }
        }
//        List<OrgAndAreaInfoDto> children = oOperationIndexMapper.getAreaByIds(orgNos);
        List<OrgAndAreaInfoDto> children = oOperationIndexMapper.getAreaByOrgId(company.getDeptId());
        if (children!=null && children.size()>0) {
            if (orgtype<4) {
                for (OrgAndAreaInfoDto child : children) {
                    IndexMapBindExDto item = new IndexMapBindExDto();
                    //上级orgNo
                    item.setpOrgNo(nowOrgCode);
                    //上级orgName
                    item.setpOrgName(nowOrgName);
                    //上级areaId
                    item.setpAreaId(areaId);
                    item.setAreaId(child.getAreaId());
                    item.setName(child.getAreaName());
                    item.setOrgNo(child.getOrgNo());
                    item.setOrgName(child.getOrgName());
                    condition.put("subOrgNo", child.getOrgNo());
                    List<String> orgs =new ArrayList<>();
                    if(child.getAreaName().equals("城厢区")){
                        orgs.add("354040102");
                        orgs.add("354040206");
                        orgs.add("354040101");
                    }else if(child.getAreaName().equals("荔城区")){
                        orgs.add("354040103");
                        orgs.add("354040106");
                    }else if(child.getAreaName().equals("梅列区")){
                        orgs.add("354080101");
                    }else if(child.getAreaName().equals("三元区")){
                        orgs.add("354080101");
                    }else if(child.getAreaName().equals("建阳区")){
                        orgs.add("3540906");
                        orgs.add("354090601");
                        orgs.add("354090602");
                        orgs.add("354090604");
                        orgs.add("354090605");
                        orgs.add("354090606");
                        orgs.add("354090607");
                        orgs.add("354090609");
                        orgs.add("354090610");
                        orgs.add("354090611");
                    }else if(child.getAreaName().equals("蕉城区")){
                        orgs.add("354030103");
                        orgs.add("354030105");
                        orgs.add("354030106");
                        orgs.add("354030108");
                        orgs.add("354030110");
                        orgs.add("354030113");
                    }else if(child.getAreaName().equals("龙文区")){
                        orgs.add("354060210");
                        orgs.add("354060211");
                        orgs.add("354060203");
                        orgs.add("354060204");
                        orgs.add("354060205");
                    }else {
                        orgs.add(child.getOrgNo());
                    }

                    condition.put("orgs", orgs);




                    List<IndexMapBindDto> datas = oOperationIndexMapper.indexMapBind(condition);
                    if (datas!=null && datas.size()>0) {
                        IndexMapBindDto data = datas.get(0);
                        BigDecimal rate = data.getRate();
                        String rateStr = PercentUtils.getPercentNum(rate);
                        Long nCons = data.getnCons();
                        Long bindAllNum = data.getBindAllNum();
                        item.setRate(rate);
                        item.setRateStr(rateStr);
                        item.setnCons(nCons);
                        item.setBindAllNum(bindAllNum);
                        if (null != rate) {
                            item.setValue(rate.multiply(new BigDecimal(100)));
                        }
//                    Map<String, Object> value = new HashMap<>();
//                    value.put("rate", rate);
//                    value.put("rateStr", rateStr);
//                    value.put("nCons", nCons);
//                    value.put("bindAllNum", bindAllNum);
//                    item.setValue(value);
                    }
                    result.add(item);
                }
            }else {
                for (OrgAndAreaInfoDto child : children) {
                    if(!orgNo.equals(child.getOrgNo())){
                        continue;
                    }
                    IndexMapBindExDto item = new IndexMapBindExDto();
                    //上级orgNo
                    item.setpOrgNo(nowOrgCode);
                    //上级orgName
                    item.setpOrgName(nowOrgName);
                    //上级areaId
                    item.setpAreaId(areaId);
                    item.setAreaId(child.getAreaId());
                    item.setName(child.getAreaName());
                    item.setOrgNo(child.getOrgNo());
                    item.setOrgName(child.getOrgName());
                    condition.put("subOrgNo", child.getOrgNo());
                    List<String> orgs =new ArrayList<>();
                    if(child.getAreaName().equals("城厢区")){
                        orgs.add("354040102");
                        orgs.add("354040206");
                        orgs.add("354040101");
                    }else if(child.getAreaName().equals("荔城区")){
                        orgs.add("354040103");
                        orgs.add("354040106");
                    }else if(child.getAreaName().equals("梅列区")){
                        orgs.add("354080101");
                    }else if(child.getAreaName().equals("三元区")){
                        orgs.add("354080101");
                    }else if(child.getAreaName().equals("建阳区")){
                        orgs.add("3540906");
                        orgs.add("354090601");
                        orgs.add("354090602");
                        orgs.add("354090604");
                        orgs.add("354090605");
                        orgs.add("354090606");
                        orgs.add("354090607");
                        orgs.add("354090609");
                        orgs.add("354090610");
                        orgs.add("354090611");
                    }else if(child.getAreaName().equals("蕉城区")){
                        orgs.add("354030103");
                        orgs.add("354030105");
                        orgs.add("354030106");
                        orgs.add("354030108");
                        orgs.add("354030110");
                        orgs.add("354030113");
                    }else if(child.getAreaName().equals("龙文区")){
                        orgs.add("354060210");
                        orgs.add("354060211");
                        orgs.add("354060203");
                        orgs.add("354060204");
                        orgs.add("354060205");
                    }else {
                        orgs.add(child.getOrgNo());
                    }

                    condition.put("orgs", orgs);


                    if (orgNo.equals(child.getOrgNo())) {
                        item.setSelected(true);
                        List<IndexMapBindDto> datas = oOperationIndexMapper.indexMapBind(condition);
                        if (datas!=null && datas.size()>0) {
                            IndexMapBindDto data = datas.get(0);
                            BigDecimal rate = data.getRate();
                            String rateStr = PercentUtils.getPercentNum(rate);
                            Long nCons = data.getnCons();
                            Long bindAllNum = data.getBindAllNum();
                            item.setRate(rate);
                            item.setRateStr(rateStr);
                            item.setnCons(nCons);
                            item.setBindAllNum(bindAllNum);
                            if (null != rate) {
                                item.setValue(rate.multiply(new BigDecimal(100)));
                            }
//                    Map<String, Object> value = new HashMap<>();
//                    value.put("rate", rate);
//                    value.put("rateStr", rateStr);
//                    value.put("nCons", nCons);
//                    value.put("bindAllNum", bindAllNum);
//                    item.setValue(value);
                        }
                    }
                    result.add(item);
                }
            }
        }
        if (!"1".equals(type) && !"2".equals(type)) {
            //县公司无下一级
            for (IndexMapBindExDto item : result) {
                item.setLevelFlag(0);
            }
        }
        if ("2".equals(type)) {
            //省
            for (IndexMapBindExDto item : result) {
                item.setNowLevel(1);
            }
        }else if ("3".equals(type)) {
            //地市
            for (IndexMapBindExDto item : result) {
                item.setNowLevel(2);
            }
        }else {
            //区县
            for (IndexMapBindExDto item : result) {
                item.setNowLevel(3);
            }
        }
        return result;
    }
}
